﻿@using BootstrapBlazor.Components
@using TrackSystem.Entities.Dtos.Slave
@using TrackSystem.Entities.Extensions
@using TrackSystem.Entities.Models


<div class="table-responsive">
    <table class="table custom-table">
        <thead>
        <tr>
            <th scope="col">
                <i class="fas fa-hashtag me-1"></i>
                序号
            </th>
            <th scope="col">
                <i class="fas fa-tag me-2"></i>
                    参数名称
            </th>
            <th scope="col">
                <i class="fas fa-tag me-4"></i>
                修改值
            </th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td><span class="badge bg-primary">1</span></td>
            <td>端口名称</td>
            <td>
                <BootstrapInput @bind-Value="@Slave.Name" />
            </td>
        </tr>
        <tr>
            <td><span class="badge bg-primary">2</span></td>
            <td>端口描述</td>
            <td>
                <BootstrapInput @bind-Value="@Slave.Description" />
            </td>
        </tr>
        <tr>
            <td><span class="badge bg-primary">3</span></td>
            <td>协议类型</td>
            <td>
                @foreach (var item in _ptlItems)
                {
                    var clasName = item.Key != Slave.Protocol ? "apple-btn-small" : "apple-btn-small apple-btn-sensor";
                    <Button class=@clasName
                            OnClick="@(() => OnPtlChanged(item.Key))">@item.Value</Button>
                }
            </td>
        </tr>
        <tr>
            <td><span class="badge bg-success">4</span></td>
            <td>端口类型</td>
            <td>
                @foreach (var item in _portItems)
                {
                    var pClass = item.Key != Slave.PortCategory ? "apple-btn-small" : "apple-btn-small apple-btn-sensor";
                    <Button class=@pClass OnClick="@(() => OnPortChanged(item.Key))">@item.Value</Button>
                }
            </td>
        </tr>
        @if (Slave.PortCategory != PortCategory.SRIAL)
        {
            <tr>
                <td><span class="badge bg-info">5</span></td>
                <td>本机地址</td>
                <td>
                    <BootstrapInputGroup>
                        <BootstrapInput @bind-Value="@Slave.LocalIP"/>
                        <BootstrapInputNumber @bind-Value="@Slave.LocalPort"/>
                    </BootstrapInputGroup>
                </td>
            </tr>
                @if (Slave.PortCategory == PortCategory.UDP)
            {
                <tr>
                    <td><span class="badge bg-info">5</span></td>
                    <td>远程地址</td>
                    <td>
                        <BootstrapInputGroup>
                            <BootstrapInput @bind-Value="@Slave.RemoteIP" />
                            <BootstrapInputNumber @bind-Value="@Slave.RemotePort" />
                        </BootstrapInputGroup>
                    </td>
                </tr>
            }
        }
        else
        {
            <tr>
                <td><span class="badge bg-info">5</span></td>
                <td>串口参数</td>
                <td>
                    <BootstrapInputGroup>
                            <BootstrapInput @bind-Value="@Slave.Comx"></BootstrapInput>
                            <BootstrapInputNumber @bind-Value="@Slave.BaudRate"></BootstrapInputNumber>
                            <Dropdown TValue="string" Items="_serialParamsItems"
                                        OnSelectedItemChanged="@OnParamChanged"
                                        Color="Color.Secondary"
                                        IsFixedButtonText="true" />
                    </BootstrapInputGroup>
                </td>
            </tr>
        }

        <tr>
            <td><span class="badge bg-dark">6</span></td>
            <td>
                    只读存储器地址表
            </td>
            <td>
                <BootstrapInputGroup>
                    <Dropdown TValue="string"
                                  Items="RegNames"
                                OnSelectedItemChanged="@((a) => OnRoInsertReg(a))"
                                Color="Color.Info"/>
                    <Textarea PlaceHolder="请输入只读存储器列表..." rows="4" @bind-Value="@Slave.RoList" />
                </BootstrapInputGroup>
            </td>
        </tr>

        @if (Slave.Protocol != SlaveProtocol.USERDEFINED)
        {
            <tr>
                <td><span class="badge bg-secondary">7</span></td>
                <td>
                    读写存储器地址表
                </td>
                <td>
                    <BootstrapInputGroup>
                            <Dropdown TValue="string"
                                      Items="RegNames"
                                        OnSelectedItemChanged="@((a) => OnRwInsertReg(a))"
                                        Color="Color.Secondary"/>
                            <Textarea PlaceHolder="请输入读写存储器列表..." rows="4" @bind-Value="@Slave.RwList" />
                    </BootstrapInputGroup>
                </td>
            </tr>

            <tr>
                <td><span class="badge bg-danger">8</span></td>
                <td>站地址</td>
                <td>
                    <BootstrapInputNumber @bind-Value="@Slave.Station"/>
                </td>
            </tr>
        }
        else
        {
            <tr>
                <td><span class="badge bg-primary">8</span></td>
                <td>协议定义</td>
                <td>
                    <BootstrapInput @bind-Value="@Slave.ProtocolDefine"/>
                </td>
            </tr>
            <tr>
                <td><span class="badge bg-success">9</span></td>
                <td>循环周期</td>
                <td>
                    <BootstrapInputNumber @bind-Value="@Slave.Cycle"/>
                </td>
            </tr>
        }
        </tbody>
    </table>
</div>



@code
{
    [Parameter]
    public SlaveDto Slave { get; set; }


    [Parameter]
    public IEnumerable<SelectedItem> RegNames { get; set; }


    private readonly Dictionary<SlaveProtocol, string> _ptlItems = new()
        {
            {SlaveProtocol.Modbus_RTU, "MODBUS-RTU"},
             {SlaveProtocol.Modbus_TCP, "MODBUS-TCP"},
            {SlaveProtocol.USERDEFINED, "自定义"},
        };

    private readonly Dictionary<PortCategory, string> _portItems = new()
        {
            {PortCategory.TCP,  "TCP" },
            {PortCategory.UDP,  "UDP" },
            {PortCategory.SRIAL, "串口" }
        };

    private readonly SelectedItem[] _serialParamsItems =
    [
        new ("1", "N-8-1"),
            new ("2", "E-8-1"),
            new ("3", "O-8-1"),
            new ("4", "N-7-1"),
            new ("5", "E-7-1"),
            new ("6", "O-7-1"),
            new ("7", "N-7-1.5"),
            new ("8", "E-7-1.5"),
            new ("9", "O-7-1.5"),
    ];



    private Task OnParamChanged(SelectedItem item)
    {
        Slave.SerialParams = item.Text;
        return Task.CompletedTask;
    }

    private Task OnRoInsertReg(SelectedItem item)
    {
        Slave.RoList += string.IsNullOrEmpty(Slave.RoList) ? item.Text : ("," + item.Text);
        StateHasChanged();
        return Task.CompletedTask;
    }

    private Task OnRwInsertReg(SelectedItem item)
    {
        Slave.RwList += string.IsNullOrEmpty(Slave.RwList) ? item.Text : ("," + item.Text);
        StateHasChanged();
        return Task.CompletedTask;
    }

    private Task OnPtlChanged(SlaveProtocol e)
    {
        Slave.Protocol = e;
        return Task.CompletedTask;
    }

    private Task OnPortChanged(PortCategory e)
    {
        Slave.PortCategory = e;
        return Task.CompletedTask;
    }


}
